package com.lzy.util;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.util.StringUtils;

/**
 * 汉字，拼音相互转换类
 *
 * @author lzy by 2019/09/10
 */
@Slf4j
public class HanyuPinyinHelper {

    /**
     * 将文字转为汉语拼音
     *
     * @param chineseLanguage 要转成拼音的中文
     * @return 转换后的拼音
     */
    public String toHanyuPinyin(String chineseLanguage) {
        char[] clChars = chineseLanguage.trim().toCharArray();
        StringBuilder hanyupinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        //输出拼音全部小写
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        //不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            for (int i = 0; i < clChars.length; i++) {
                // 如果字符是中文,则将中文转为汉语拼音
                if (String.valueOf(clChars[i]).matches("[\u4e00-\u9fa5]+")) {
                    hanyupinyin.append(PinyinHelper.toHanyuPinyinStringArray(clChars[i], defaultFormat)[0]);
                } else {// 如果字符不是中文,则不转换
                    hanyupinyin.append(clChars[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.error("转换拼音失败", e);
        }
        return hanyupinyin.toString();
    }

    public static String getFirstLettersUp(String chineseLanguage) {
        return getFirstLetters(chineseLanguage, HanyuPinyinCaseType.UPPERCASE);
    }

    public static String getFirstLettersLo(String chineseLanguage) {
        return getFirstLetters(chineseLanguage, HanyuPinyinCaseType.LOWERCASE);
    }

    public static String getFirstLetters(String chineseLanguage, HanyuPinyinCaseType caseType) {
        char[] clChars = chineseLanguage.trim().toCharArray();
        StringBuilder hanyupinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 输出拼音全部大写
        defaultFormat.setCaseType(caseType);
        // 不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        try {
            for (int i = 0; i < clChars.length; i++) {
                String str = String.valueOf(clChars[i]);
                //如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                if (str.matches("[\u4e00-\u9fa5]+")) {
                    hanyupinyin.append(PinyinHelper.toHanyuPinyinStringArray(clChars[i], defaultFormat)[0].substring(0, 1));
                } else if (str.matches("[0-9]+")) {
                    //如果字符是数字,取数字
                    hanyupinyin.append(clChars[i]);
                    //如果字符是字母,取字母
                } else if (str.matches("[a-zA-Z]+")) {
                    //否则不转换
                    hanyupinyin.append(clChars[i]);
                } else {
                    // 如果是标点符号的话，带着
                    hanyupinyin.append(clChars[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.error("转换拼音失败", e);
        }
        return hanyupinyin.toString();
    }

    public static String getPinyinString(String chineseLanguage) {
        char[] clChars = chineseLanguage.trim().toCharArray();
        StringBuilder hanyupinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        //输出拼音全部大写
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        try {
            for (char clChar : clChars) {
                String str = String.valueOf(clChar);

                if (str.matches("[\u4e00-\u9fa5]+")) {
                    //如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                    hanyupinyin.append(PinyinHelper.toHanyuPinyinStringArray(clChar, defaultFormat)[0]);
                } else if (str.matches("[0-9]+")) {
                    // 如果字符是数字,取数字
                    hanyupinyin.append(clChar);
                } else if (str.matches("[a-zA-Z]+")) {
                    // 如果字符是字母,取字母
                    hanyupinyin.append(clChar);
                }

            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            System.out.println("");
            log.error("字符转换汉语拼音失败", e);
        }
        return hanyupinyin.toString();
    }

    /**
     * 取第一个汉字的第一个字符，非字符再去下一个* ,不存在，则返回#@Title: getFirstLetter
     *
     * @param chineseLanguage 需要转换的字符串
     * @return
     */
    public static String getFirstLetter(String chineseLanguage) {

        String result = "";

        try {
            char[] chars = chineseLanguage.trim().toCharArray();
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
            // 输出拼音全部大写
            defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
            // 不带声调
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            for (int i = 0; i < chars.length; i++) {
                String str = String.valueOf(chars[i]);
                // 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                if (str.matches("[\u4e00-\u9fa5]+")) {
                    // 如果字符是数字,取数字
                    result = PinyinHelper.toHanyuPinyinStringArray(chars[i], defaultFormat)[i].substring(0, 1);
                } else if (str.matches("[a-zA-Z]+")) {
                    // 如果字符是字母,取字母
                    result += chars[0];
                    result = result.toUpperCase();
                } else {
                    // 否则不转换
                    result = "";
                }
                if (!StringUtils.isEmpty(result)) {
                    break;
                }
            }
        } catch (Exception e) {
            log.error("拼音获取首字母异常", e);
            return "#";
        }
        return StringUtils.isEmpty(result) ? "#" : result;
    }

    /**
     * 判断字符是否是字母
     */
    public static boolean isLetter(String s) {
        char c = s.charAt(0);
        return ((int) c >= 65 && (int) c <= 90) || ((int) c >= 97 && (int) c <= 122);
    }

}